<template>
  <el-dialog
    :title="!houses.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="学校名称" prop="schoolName">
      <el-input v-model="houses.schoolName" placeholder="学校名称"></el-input>
    </el-form-item>
    <el-form-item label="区域" prop="area">
      <el-input v-model="houses.area" placeholder="区域"></el-input>
    </el-form-item>
    <el-form-item label="省份id" prop="provinceId">
      <el-input v-model="houses.provinceId" placeholder="省份id"></el-input>
    </el-form-item>
    <el-form-item label="市级id" prop="city">
      <el-input v-model="houses.city" placeholder="市级id"></el-input>
    </el-form-item>
    <el-form-item label="县级id" prop="county">
      <el-input v-model="houses.county" placeholder="县级id"></el-input>
    </el-form-item>
    <el-form-item label="学校地址" prop="addr">
      <el-input v-model="houses.addr" placeholder="学校地址"></el-input>
    </el-form-item>
    <el-form-item label="学校性质" prop="nature">
      <el-input v-model="houses.nature" placeholder="学校性质"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="houses.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
      <el-input v-model="houses.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="类型id" prop="typeId">
      <el-input v-model="houses.typeId" placeholder="类型id"></el-input>
    </el-form-item>
    <el-form-item label="缩略图" prop="images">
      <el-input v-model="houses.images" placeholder="缩略图"></el-input>
    </el-form-item>
    <el-form-item label="介绍" prop="recommoend">
      <el-input v-model="houses.recommoend" placeholder="介绍"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        houses: {
          id: 0,
          schoolName: '',
          area: '',
          provinceId: '',
          city: '',
          county: '',
          addr: '',
          nature: '',
          longitude: '',
          latitude: '',
          typeId: '',
          images: '',
          recommoend: ''
        },
        dataRule: {
          schoolName: [
            { required: true, message: '学校名称不能为空', trigger: 'blur' }
          ],
          area: [
            { required: true, message: '区域不能为空', trigger: 'blur' }
          ],
          provinceId: [
            { required: true, message: '省份id不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市级id不能为空', trigger: 'blur' }
          ],
          county: [
            { required: true, message: '县级id不能为空', trigger: 'blur' }
          ],
          addr: [
            { required: true, message: '学校地址不能为空', trigger: 'blur' }
          ],
          nature: [
            { required: true, message: '学校性质不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          typeId: [
            { required: true, message: '类型id不能为空', trigger: 'blur' }
          ],
          images: [
            { required: true, message: '缩略图不能为空', trigger: 'blur' }
          ],
          recommoend: [
            { required: true, message: '介绍不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.id) {
            this.$http({
              url: this.$http.adornUrl(`/area/school/info/${this.houses.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.schoolName = data.school.schoolName
                this.houses.area = data.school.area
                this.houses.provinceId = data.school.provinceId
                this.houses.city = data.school.city
                this.houses.county = data.school.county
                this.houses.addr = data.school.addr
                this.houses.nature = data.school.nature
                this.houses.longitude = data.school.longitude
                this.houses.latitude = data.school.latitude
                this.houses.typeId = data.school.typeId
                this.houses.images = data.school.images
                this.houses.recommoend = data.school.recommoend
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/area/school/${!this.houses.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.houses.id || undefined,
                'schoolName': this.houses.schoolName,
                'area': this.houses.area,
                'provinceId': this.houses.provinceId,
                'city': this.houses.city,
                'county': this.houses.county,
                'addr': this.houses.addr,
                'nature': this.houses.nature,
                'longitude': this.houses.longitude,
                'latitude': this.houses.latitude,
                'typeId': this.houses.typeId,
                'images': this.houses.images,
                'recommoend': this.houses.recommoend
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
